﻿namespace TheSafety
{
    using Agent;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Windows;

    public class NewsManager : DependencyObject
    {
        private static List<DateTime> a = new List<DateTime>();
        [CompilerGenerated]
        private static Action b;
        [CompilerGenerated]
        private static ParameterizedThreadStart c;
        [CompilerGenerated]
        private static ParameterizedThreadStart d;

        [CompilerGenerated]
        private static void a()
        {
            if (DateTime.Now.ToString("HH:mm") == ConfigManager.b("updatein"))
            {
                ConfigManager.a("update_news", (double) 60000.0);
                b();
            }
        }

        [CompilerGenerated]
        private static void a(object A_0)
        {
            List<NewsItem> list = new List<NewsItem>();
            if (((ConfigManager.b("updateifactive") == "1") && (ConnectionButton.c() != null)) || (ConfigManager.b("updateifactive") == "0"))
            {
                if (ConfigManager.b("notifymeimportant") == "1")
                {
                    if (list != null)
                    {
                        list.Clear();
                    }
                    list = null;
                    list = WebRequestManager.f();
                    foreach (NewsItem item in list)
                    {
                        item.a(item.b().ToString() + Environment.NewLine + item.d());
                        a(item, true);
                    }
                }
                if (ConfigManager.b("notifymeprolong") == "1")
                {
                    list.Clear();
                    list = null;
                    foreach (NewsItem item2 in WebRequestManager.e())
                    {
                        if (!a.Contains(item2.b()))
                        {
                            a.Add(item2.b());
                            TimeSpan span = item2.b().Subtract(DateTime.Now.ToUniversalTime());
                            if (span.TotalDays < Convert.ToInt32(ConfigManager.b("notifydaysbefore")))
                            {
                                item2.a(a(item2, span));
                                item2.a(DateTime.Now.ToString() + Environment.NewLine + item2.d());
                                a(item2, true);
                            }
                        }
                    }
                }
            }
        }

        private static void a(NewsItem A_0, bool A_1)
        {
            Action action = null;
            EventHandler handler = null;
            EventHandler handler2 = null;
            NewsItem a = A_0;
            try
            {
                <>c__DisplayClassd classd;
                if (a.a() == NewsState.New)
                {
                    a.a(NewsState.Hidden);
                    if (action == null)
                    {
                        action = new Action(classd.b);
                    }
                    ConfigManager.a(a.b().ToFileTime().ToString(), (ConfigManager.b("notifyrepeateeach") == "Never") ? 2147483647.0 : (double.Parse(ConfigManager.b("notifyrepeateeach")) * 60000.0), action);
                    Thread.Sleep(0x1388);
                }
                if (!a.c() && A_1)
                {
                    AgentMessageEventArgs args = new AgentMessageEventArgs();
                    args.a(NotificationType.Info);
                    args.a(a.d());
                    if (handler == null)
                    {
                        handler = new EventHandler(classd.c);
                    }
                    args.b(handler);
                    if (handler2 == null)
                    {
                        handler2 = new EventHandler(classd.b);
                    }
                    args.a(handler2);
                    args.a((ConfigManager.b("notifywithin") == "Forever") ? 0x7fffffff : (int.Parse(ConfigManager.b("notifywithin")) * 0x3e8));
                    ConfigManager.m().b(null, args);
                    if (ConfigManager.b("notifyrepeateeach") == "Never")
                    {
                        if (a.a() != NewsState.Old)
                        {
                            a.a(NewsState.Old);
                            ConfigManager.e(a.b().ToFileTime().ToString());
                        }
                        a.a(true);
                    }
                }
            }
            catch (Exception exception)
            {
                ConfigManager.a("ShowNews", exception);
            }
        }

        private static string a(NewsItem A_0, TimeSpan A_1)
        {
            string str = null;
            string str2 = "";
            try
            {
                string[] strArray = ConfigManager.c(ConfigManager.q() + "/config");
                if (strArray != null)
                {
                    foreach (string str3 in strArray)
                    {
                        FileInfo info = new FileInfo(str3);
                        int index = info.Name.IndexOf(A_0.d() + "_", 0);
                        if (index > 0)
                        {
                            string oldValue = info.Name.Substring(0, index) + A_0.d() + "_";
                            str = info.Name.Replace(".ovpn", "").Replace(oldValue, "");
                            break;
                        }
                    }
                }
                str2 = string.Concat(new object[] { 
                    e.c("ExpireSoon"), "\n", (str != null) ? str : A_0.d(), "\n", e.c("EndDate"), A_0.b().ToShortDateString(), "\n", e.c("Left"), " ", A_1.Days, e.c("d"), " ", A_1.Hours, e.c("h"), " ", A_1.Minutes, 
                    e.c("m")
                 });
            }
            catch (Exception exception)
            {
                ConfigManager.a("FormatProlongMessage", exception);
            }
            return str2;
        }

        public static void b()
        {
            if (d == null)
            {
                d = new ParameterizedThreadStart(NewsManager.a);
            }
            new Thread(d) { IsBackground = true }.Start(null);
        }

        [CompilerGenerated]
        private static void b(object A_0)
        {
            new List<NewsItem>().Clear();
            List<NewsItem> list = null;
            list = WebRequestManager.f();
            if (ConfigManager.b("notifymeimportant") == "1")
            {
                foreach (NewsItem item in list)
                {
                    item.a(item.b().ToString() + Environment.NewLine + item.d());
                    a(item, true);
                }
            }
            list.Clear();
            list = null;
            list = WebRequestManager.e();
            if (ConfigManager.b("notifymeprolong") == "1")
            {
                foreach (NewsItem item2 in list)
                {
                    if (!a.Contains(item2.b()))
                    {
                        a.Add(item2.b());
                        TimeSpan span = item2.b().Subtract(DateTime.Now.ToUniversalTime());
                        if (span.TotalDays < Convert.ToInt32(ConfigManager.b("notifydaysbefore")))
                        {
                            item2.a(a(item2, span));
                            item2.a(DateTime.Now.ToString() + Environment.NewLine + item2.d());
                            a(item2, false);
                        }
                    }
                }
            }
        }

        public static void c()
        {
            if (c == null)
            {
                c = new ParameterizedThreadStart(NewsManager.b);
            }
            new Thread(c) { IsBackground = true }.Start(null);
        }

        public static void d()
        {
            b();
            string str = ConfigManager.b("schedulerupdate");
            if (str != null)
            {
                if (!(str == "0"))
                {
                    if (!(str == "1"))
                    {
                        if (!(str == "2"))
                        {
                            return;
                        }
                        return;
                    }
                }
                else
                {
                    ConfigManager.a("update_news", (double.Parse(ConfigManager.b("updateeach")) * 60.0) * 1000.0, new Action(NewsManager.b));
                    return;
                }
                if (b == null)
                {
                    b = new Action(NewsManager.a);
                }
                ConfigManager.a("update_news", 1000.0, b);
            }
        }
    }
}

